import axiosService from "@/utils/request.js";
import {ElMessage} from "element-plus";
import {nextTick} from "vue";

export function loginService(loginForm) {
    return axiosService({
        url: "/admin/user/login",
        method: "post",
        data: loginForm
    })
}

export function registerService(registerForm) {
    return axiosService({
        url: "/admin/user/register",
        method: "post",
        data: registerForm
    })
}

export function getUserNameService() {
    return axiosService({
        url: "/admin/user/get-username",
        method: "post"
    })
}

export async function validateService(username, password) {
    if (username.toString().trim() === '') {
        ElMessage({
            message: '用户名不能为空',
            type: 'warning',
        })
        // 聚焦到用户名输入框
        await nextTick(() => {
            const usernameInput = document.querySelector('#username');
            if (usernameInput) {
                usernameInput.focus();
            }
        });
        return false;
    }

    if (password.toString().trim() === '') {
        ElMessage({
            message: '密码不能为空',
            type: 'warning',
        })
        // 聚焦到用户名输入框
        await nextTick(() => {
            const passwordInput = document.querySelector('#password');
            if (passwordInput) {
                passwordInput.focus();
            }
        });
        return false;
    }
    return true;
}

export function logOutService() {
    return axiosService({
        url: "/admin/user/logout",
        method: "post"
    })
}
